<?php
function url_build($param, $type = 0)
{
  $res = '';
  $arr_main = array();
  $arr = array();

  foreach ($param as $key => $val) {
    if (empty($val)) {
      continue;
    }

    if ($type && in_array($key, ['s', 'm', 'c'])) {
      $arr_main[$key] = $val;
      continue;
    }

    $arr[] = str_safe($key) . '=' . (is_numeric($val) ? intval($val) : str_safe($val));
  }

  if ($type) {
    krsort($arr_main);
    $res .= '/' . array_str($arr_main, '/') . '/';
  }

  if (!empty($arr)) {
    $res .= '?' . array_str($arr, '&');
  }

  unset($arr_main);
  unset($arr);

  return $res;
}
/*
function url_build($param, $type = 0)
{
  $res = '';
  $data = array();

  foreach ($param as $key => $val) {
    if (empty($val)) {
      continue;
    }

    if ($type) {
      $res .= $val . '-';
    } else {
      $res .= $key . '=' . $val . '&';
    }
  }

  return $type ? rtrim($res, '-') . '.' . REWRITE_EX : '?' . rtrim($res, '&');
}
*/

function check_url_absolute($url)
{
  if (
    false !== strpos($url, 'http:') ||
    false !== strpos($url, 'https:') ||
    0 == strpos($url, '/')
  ) {
    return true;
  }
  return false;
}

//获取访问者真实IP
function get_ip()
{
  if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    //check ip FROM share internet
    $ip = $_SERVER['HTTP_CLIENT_IP'];
  } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    //to check ip is pass FROM proxy
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  } else {
    $ip = $_SERVER['REMOTE_ADDR'];
  }
  return $ip;
}

// 获取当前完整URL
function get_url_real()
{
  return $_SERVER['SERVER_NAME'] . (!empty($_SERVER['SERVER_PORT']) && @$_SERVER['SERVER_PORT'] != '80' ? ":" . @$_SERVER['SERVER_PORT'] : "") . @$_SERVER['REQUEST_URI'];
}

function get_url()
{
  return $_SERVER['SERVER_NAME'] . (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != '80' ? ":" . $_SERVER['SERVER_PORT'] : "") . get_uri();
}

function set_url_back($str = '')
{
  if (!empty($str)) {
    set_cookie('cms[url_back]', $str, gmtime() + COOKIE_TIMEOUT);
    return;
  }

  set_cookie('cms[url_back]', get_uri(), gmtime() + COOKIE_TIMEOUT);
  return;
}

function set_url_back_empty()
{
  set_cookie('cms[url_back]', '', gmtime() - COOKIE_EXPIRE);
}

function get_url_back()
{
  return $_COOKIE['cms']['url_back'];
}

function get_uri()
{
  $res = '';
  if (empty($_SERVER['REQUEST_URI'])) {
    return false;
  }

  if (REWRITE && !is_admin()) {
    $arr = array();
    preg_match("/\/(\w+)\/(\w+)?\/?(\w+)?\/?(\w+\.[html|htm|php|asp|aspx|jsp|phtml]+)?\??(.*)?/", $_SERVER['REQUEST_URI'], $arr);
    // array(6) { [0] => string(33) "/user/index/login/index.html?id=5" [1] => string(4) "user" [2] => string(5) "index" [3] => string(5) "login" [4] => string(10) "index.html" [5] => string(4) "id=5" }

    if (!empty($arr[5])) {
      $res = $res . '&' . $arr[5];
    }

    if (!empty($arr[3])) {
      $res = '/?s=' . str_safe($arr[1]) . '&m=' . str_safe($arr[2]) . '&c=' . str_safe($arr[3]) . $res;
    } elseif (!empty($arr[2])) {
      $res = '/?s=' . str_safe($arr[1]) . '&m=' . str_safe($arr[2]) . $res;
    } elseif (!empty($arr[1])) {
      $res = '/?s=index&m=' . str_safe($arr[1]) . $res;
    }
  }

  return iif($res, $_SERVER['REQUEST_URI']);

  /*
  return isset($_SERVER['REQUEST_URI'])
    ? $_SERVER['REQUEST_URI']
    : (isset($_SERVER['argv']) ? $_SERVER['argv'][0] : $_SERVER['QUERY_STRING']);
    */
}
function get_param()
{
  $uri = get_uri();
  $arr_param = array();

  if (strpos($uri, '?') !== false) {
    list($path_string, $param_string) = explode('?', $uri);
  } else {
    $param_string = $uri;
  }

  if (strpos($param_string, '&') !== false) {
    $arr = explode('&', $param_string);
    foreach ($arr as $value) {
      list($key, $val) = explode('=', $value);
      $arr_param[$key] = $val;
    }
    return $arr_param;
  }

  if (strpos($param_string, '=') !== false) {
    list($key, $val) = explode('=', $param_string);
    $arr_param[$key] = $val;
    return $arr_param;
  }

  unset($arr_param);
  return false;
}

// 模块&控制器函数
function M($s = '', $m = '', $c = '', $id = 0, $force = false)
{
  $param = array('s' => str_safe($s), 'm' => str_safe($m), 'c' => str_safe($c), 'id' => intval($id));

  return REWRITE && (!is_admin() || $force) ? url_build($param, 1) : url_build($param);
}
/*
// 模块&控制器函数
function M($s = '', $m = '', $c = '', $id = '', $link = '')
{
  if ('' != $link) {
    return $link;
  }

  $param = array(
    's' => $s,
    // 's' => '' == $s ? ('app' != DEFAULT_MODULE ? DEFAULT_MODULE : '') : $s,
    'm' => $m,
    'c' => $c,
    'id' => $id,
  );

  return REWRITE && !is_admin() ? url_build($param, 1) : url_build($param);
}
*/

function M_input($arr_esc = ['keyword', 'page'])
{
  $str = '';
  $arr_param = get_param();

  // $arr = array( 's' => iif(str_safe($_GET['s']), DEFAULT_MODULE), 'm' => iif(str_safe($_GET['m']), 'index'), 'c' => iif(str_safe($_GET['c']), 'index'), 'id' => iif(intval($_REQUEST['id']), 0), );

  foreach ($arr_param as $key => $value) {
    if (in_array($key, $arr_esc)) continue;
    $str .= '<input type="hidden" name="' . $key . '" value="' . $value . '">';
  }

  return $str;
}

function M_string($arr_esc = ['keyword', 'page'])
{
  $arr = array();
  $arr_param = get_param();
  if ($arr_param === false) return false;

  foreach ($arr_param as $key => $val) {
    if (in_array($key, $arr_esc)) continue;
    $arr[] = $key . '=' . $val;
  }

  return array_str($arr, '&');
}

//频道链接地址
function c_url($id, $link = '')
{
  return empty($link) ? M('index', 'channel', '', $id, true) : $link;
}

//详情链接地址
function d_url($id, $link = '')
{
  return  empty($link) ? M('index', 'detail', '', $id) : $link;
}

function http_get($url)
{
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_TIMEOUT, 200);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
  curl_setopt($curl, CURLOPT_URL, $url);
  $output = curl_exec($curl);
  if (curl_errno($curl)) {
    echo 'Error: ' . curl_error($curl);
  }
  curl_close($curl);
  return $output;
}

function http_post($url, $data)
{
  // 模拟提交数据函数
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  if (class_exists('\\CURLFile')) {
    // curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, true);
  } else {
    if (defined('CURLOPT_SAFE_UPLOAD')) {
      curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
    }
  }
  curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
  $output = curl_exec($curl);
  if (curl_errno($curl)) {
    echo 'Error: ' . curl_error($curl);
  }
  curl_close($curl);
  return $output;
}

function https_get($url)
{
  // 模拟提交数据函数
  $curl = curl_init();
  // 启动一个CURL会话
  curl_setopt($curl, CURLOPT_URL, $url);
  // 要访问的地址
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  // 对认证证书来源的检查
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
  // 从证书中检查SSL加密算法是否存在
  curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  // 模拟用户使用的浏览器
  @curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  // 使用自动跳转
  curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  // 自动设置Referer
  curl_setopt($curl, CURLOPT_TIMEOUT, 200);
  // 设置超时限制防止死循环
  curl_setopt($curl, CURLOPT_HEADER, 0);
  // 显示返回的Header区域内容
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  // 获取的信息以文件流的形式返回
  $output = curl_exec($curl);
  // 执行操作
  if (curl_errno($curl)) {
    echo 'Error: ' . curl_error($curl);
  }
  curl_close($curl);
  // 关闭CURL会话
  return $output;
}

function https_post($url, $data)
{
  // 模拟提交数据函数
  $curl = curl_init();
  // 启动一个CURL会话
  curl_setopt($curl, CURLOPT_URL, $url);
  // 要访问的地址
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  // 对认证证书来源的检查
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
  // 从证书中检查SSL加密算法是否存在
  curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  // 模拟用户使用的浏览器
  @curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  // 使用自动跳转
  curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  // 自动设置Referer
  curl_setopt($curl, CURLOPT_POST, 1);
  // 发送一个常规的Post请求
  curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  // Post提交的数据包
  curl_setopt($curl, CURLOPT_TIMEOUT, 200);
  // 设置超时限制防止死循环
  curl_setopt($curl, CURLOPT_HEADER, 0);
  // 显示返回的Header区域内容
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  // 获取的信息以文件流的形式返回
  $output = curl_exec($curl);
  // 执行操作
  if (curl_errno($curl)) {
    echo 'Error: ' . curl_error($curl);
  }
  curl_close($curl);
  // 关闭CURL会话
  return $output;
}
